#include <iostream>
#include <fstream>
#include <stdexcept>
#include <cmath> 
#include <vector>
#include "Function.hpp"
#include "Interpolation.hpp"

class F1_C : public Function {
public:
    double operator() (double x) const {
        return 1 / (1 + 25 * x * x);
    }
};

std::vector<double> calculateChebyshevNodes(int n) {
    std::vector<double> nodes;
    for (int k = 0; k <= n; ++k) {
        nodes.push_back(cos((2 * k + 1) * M_PI / (2 * n + 2)));
    }
    return nodes;
}

double polynomial(double x, int n) {
    std::vector<double> nodes = calculateChebyshevNodes(n);
    NewtonInterpolation newton(F1_C(), nodes);
    return newton.interpolate(x);  
}

int main() {
    for (int n = 5; n <= 20; n += 5) {
        std::ofstream file("data_C" + std::to_string(n) + ".csv");
        if (!file.is_open()) {
            std::cerr << "Failed to open file" << std::endl;
            return 1;
        }

        for (double x = -1; x <= 1; x += 0.01) {
            double y = polynomial(x, n);
            file << x << "," << y << std::endl;
        }

        file.close();
    }
    return 0; 
}